/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author cristiane
 */
public class Conexao {

    public static Connection getConnection() throws ImobiliariaDaoException, ClassNotFoundException{

        try{
            Class.forName("org.postgresql.Driver");
            String bd = "jdbc:postgresql://localhost/imobiliaria";
            String usr = "postgres";
            String psswrd = "postgres";
            return DriverManager.getConnection(bd, usr, psswrd);
        }catch (Exception e){
            throw new ImobiliariaDaoException(e.getMessage());
        }

    }

    public static void closeConnection(Connection conn, PreparedStatement stat, ResultSet rs)
            throws ImobiliariaDaoException{
        close(conn, stat, rs);
    }

    public static void closeConnection(Connection conn, PreparedStatement stat)
            throws ImobiliariaDaoException{
        close(conn, stat);
    }

    public static void closeConnection(Connection conn)
            throws ImobiliariaDaoException{
        close(conn);
    }

    private static void close(Connection conn, PreparedStatement stat, ResultSet rs) throws ImobiliariaDaoException{
        try{
            if(conn != null) conn.close();
            if(stat != null) stat.close();
            if(rs != null) rs.close();
        }catch (Exception e){
            throw new ImobiliariaDaoException(e.getMessage());
        }
    }

    private static void close(Connection conn, PreparedStatement stat) throws ImobiliariaDaoException{
        try{
            if(conn != null) conn.close();
            if(stat != null) stat.close();
        }catch(Exception e){
            throw new ImobiliariaDaoException(e.getMessage());
        }
    }

    private static void close(Connection conn) throws ImobiliariaDaoException{
        try{
            if(conn != null) conn.close();
        }catch(Exception e){
            throw new ImobiliariaDaoException(e.getMessage());
        }
    }

}
